﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ImageProcessing.Controls;

namespace ImageProcessing.LinearFilters
{
    public class FirViewModel:INotifyPropertyChanged
    {
        
        public EventHandler CancelEventHandler { get; set; }
        public EventHandler<MaskEventArgs> OkEventHandler { get; set; }
        public FirViewModel()
        {
            Height = 1;
            Width = 3;
            OkCommand = new DelegateCommand(OkExecute,CanOkExecute);
            CancelСommand = new DelegateCommand(CancelExecute);
        }

        private void OkExecute(object sender)
        {
            var handler = OkEventHandler;
            if (handler != null)
            {
                var list = new List<int>();
                foreach (NumericTextBox child in MaskGrid.Children)
                {
                    var a = child.Content as TextBox;
                    var value=0;
                    if (a != null) int.TryParse(a.Text,out value);
                    list.Add(value);
                }
                handler(this, new MaskEventArgs(Height, Width, list));
            }
        }

       

        #region Commands
        public DelegateCommand OkCommand { get; set; }
        public DelegateCommand CancelСommand { get; set; }
        private bool CanOkExecute(object sender)
        {
            return (from object child in MaskGrid.Children
                    select child as NumericTextBox into a
                    select a.Content).OfType<TextBox>().
                    All(b => !String.IsNullOrEmpty(b.Text));
        }

        private void CancelExecute(object sender)
        {
            var handler = CancelEventHandler;
            if (handler != null)
                handler(this, new EventArgs());

        }
        #endregion Commands

        private int _height;
        public int Height
        {
            get { return _height; }
            set { _height = value;
            OnPropertyChanged("Height");
            BuildGrid();
            }
        }

        private void BuildGrid()
        {
            var grid = new Grid();
            for (int i = 0; i < Height; i++)
            {
                grid.RowDefinitions.Add(new RowDefinition());
            }
            for (int i = 0; i < Width; i++)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition());
            }
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    var textBox = new NumericTextBox();
                    Grid.SetRow(textBox, i);
                    Grid.SetColumn(textBox, j);
                    grid.Children.Add(textBox);
                }
            }
            grid.MouseLeave+=GridMouseLeave;
            grid.LostFocus+=GridLostFocus;
            MaskGrid = grid;
        }

        private void GridLostFocus(object sender, RoutedEventArgs e)
        {
            OkCommand.RaiseCanExecuteChanged();
        }

        private void GridMouseLeave(object sender, MouseEventArgs e)
        {
            OkCommand.RaiseCanExecuteChanged();
        }


        private int _width;
        public int Width
        {
            get { return _width; }
            set
            {
                _width = value;
                OnPropertyChanged("Width");
                BuildGrid();
            }
        }

        private Grid _maskGrid;
        public Grid MaskGrid
        {
            get { return _maskGrid; }
            set { _maskGrid = value;
                OnPropertyChanged("MaskGrid");
            }
        }

        #region INotifyPropertyChanged parts
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

    public class MaskEventArgs : EventArgs
    {
        public MaskEventArgs(int height ,int width , List<int> mask)
        {
            Width = width;
            Height = height;
            Mask = mask;
        }
        public int Width { get; set; }
        public int Height { get; set; }
        public List<int> Mask { get; set; }
    }
}
